<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<?php
	session_start();
	if (!isset($_SESSION['name'])) {
		header("Location: login.php");
	}
	if (isset($_GET['tractorId'])){
		$id = $_GET['tractorId'];
	}
	else {
		$id = $_POST['tractorId'];
	}
	$db_connect = mysqli_connect("localhost" , "root" , "" , "tractor");
	$query = "select name,model,price,link from tractors where tractor_id = \"$id\"";
	$result = mysqli_query($db_connect,$query);
	$row =mysqli_fetch_assoc($result);
	
	if(isset($_POST['return'])){
		header("Location: index.php");
	}
	if(isset($_POST['submit']))
	{
		if(empty($_POST['name']))
		{
			echo "<p>Please insert a name</p>";
		}
		if(empty($_POST['model']))
		{
			echo "<p>Please insert a model</p>";
		}
		if(empty($_POST['price']))
		{
			echo "<p>Please insert a price</p>";
		}
	
	
		$name = $_POST['name'];
		$model = $_POST['model'];
		$price = $_POST['price'];
	
		if(isset($_SESSION['link'])){
			$link = $_SESSION['link'];
		}
		else{
			$link = $_POST['link'];
		}
	
	
		$query = "update tractors set name = \"$name\" , model =\"$model\" ,
		 price = \"$price\" ,link = \"$link\" where tractor_id = \"$id\"";
		$result = mysqli_query($db_connect,$query);
	
		if($result)
		{
			echo "successfull";
		}
		else
		{
			echo "not added";
		}
	}
	?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1251">
<title>Insert title here</title>
</head>
    <body>
    <form action="edit.php" method="post">
    	Name<input value="<?php echo $row['name']; ?>" name="name"><br/>
    	Model<input  value="<?php echo $row['model']; ?>" name="model"><br/>
    	Price<input  value="<?php echo $row['price']; ?>" name="price"><br/>
    	<input type="hidden" name="tractorId" value="<?php echo $id;?>">
    	Current picture:<img name="link" alt="choosen" src="<?php if(isset($_SESSION['link'])){
																	echo $_SESSION['link'];
																	}else echo $row['link']; ?>" style="width: 50px; height: 50px;"><br/>
		<input type="submit" name="submit" value="Edit">
		<input type="submit" name="return" value="Back"/>
	</form>
	<form action="upload_file.php?from=edit&tractorId=<?php echo $id;?>" method="post" enctype="multipart/form-data">
		<label for="file">Filename:</label>
		<input type="file" name="file" id="file"><br>
		<input type="submit" name="submit" value="Add Picture">
	</form>
    </body>
</html>